﻿#include"game.h"

void start_game()
{
	printf("******************\n");
	printf("***** 1、play ****\n");
	printf("***** 0、exit ****\n");
	printf("******************\n");
}

void play_board(int input, char board[ROW][COL])
{
	int i = 0;
	int j = 0;

	switch (input)
	{
	case 1:
		for (i = 0; i < ROW; i++)
		{
			for (j = 0; j < COL; j++)
			{
				printf(" %c ", board[i][j]);
				if (j < COL - 1)
					printf("|");
			}
			printf("\n");

			for (j = 0; j < COL && i < ROW - 1; j++)
			{
				if (i < COL - 1)
				{
					printf("---");
					if (j < COL - 1)
						printf("|");
				}
			}

			printf("\n");
		}
		break;
	case 0:
		printf("退出游戏\n");
		break;
	default:
		printf("选择错误，请重新选择\n");
		break;
	}
}

void init_board(char board[ROW][COL])
{
	int i = 0;
	int j = 0;

	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			board[i][j] = 'a';
		}
	}
}

void player_play(int row, int col, char board[ROW][COL])
{
	//玩家落子
	if (row <= ROW - 1 && row >= 0)
	{
		if (board[row][col] == 'a')
		{
			board[row][col] = '1';
		}
		else
		{
			printf("该坐标已被占用\n");
		}
	}
	else
	{
		printf("该坐标不合法，请重新输入\n");
	}
}

//电脑落子
void computer_play(char board[ROW][COL])
{
	int row = 0;
	int col = 0;
	while (1)
	{
		row = rand() % ROW;
		col = rand() % COL;

		if (board[row][col] == 'a')
		{
			board[row][col] = '2';
			break;
		}
	}
}

//判断棋盘是否满
int is_full(char board[ROW][COL])
{
	int i = 0;
	int j = 0;
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			if (board[i][j] == 'a')
			{
				return 1;
			}
		}
	}

	return 0;
}

//判断谁赢
char judge(char board[ROW][COL])
{
	int i = 0;
	int j = 0;
	for (i = 0; i < ROW; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != 'a')
		{
			return board[i][0];
		}
	}

	for (i = 0; i < ROW; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[i][0] != 'a')
		{
			return board[0][i];
		}
	}

	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[i][0] != 'a')
	{
		return board[0][0];
	}

	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[i][0] != 'a')
	{
		return board[2][0];
	}

	if (is_full(board) == 0)
	{
		return 'Q';
	}

	return 'C';
}